package com.earl.gsm.web.match;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.earl.gsm.commons.vo.DwzMessage;
import com.earl.gsm.domain.gsm.match.HistoryMatch;
import com.earl.gsm.domain.gsm.match.MatchDetail;
import com.earl.gsm.domain.gsm.match.Team;
import com.earl.gsm.domain.sercurity.user.User;
import com.earl.gsm.service.HistoryMatchService;
import com.earl.gsm.service.MatchDetailService;
import com.earl.gsm.service.TeamInfoService;
import com.earl.gsm.web.BaseController;
import com.wordnik.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/match")
//@Api(value="Person Rest Service") //修改生成的son路径
public class MatchDetailController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MatchDetailController.class);

    @Autowired
    private MatchDetailService matchService;

    @Autowired
    private TeamInfoService teamService;

    @Autowired
    private HistoryMatchService historyMatchService;

    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/all")
    @ApiOperation(value = "得到所有比赛简易信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView getAll() {
       return new ModelAndView("match/allMatch","matchList", matchService.findAll());
    }

    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/uncompelete")
    @ApiOperation(value = "得到所有比赛简易信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView uncompelete() {
        MatchDetail matchDetail = new MatchDetail();
        matchDetail.setCompelete(false);
        return new ModelAndView("match/allMatch","matchList", matchService.searchQuery(matchDetail));
    }

    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/allteam/{matchId}")
    @ApiOperation(value = "得到指定比赛id的队伍", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView allTeam(@PathVariable Long matchId) {

        MatchDetail one = matchService.findOne(matchId);
        List<Long> integers = new ArrayList<>();
        integers.add(one.getTeam1Id());
        integers.add(one.getTeam2Id());
        return new ModelAndView("match/allTeam","teamList", teamService.findByList(integers));
    }

    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/history")
    @ApiOperation(value = "得到所有比赛简易信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView allHistoryMatch() {
        return new ModelAndView("match/allHistoryMatch","matchList", historyMatchService.findAll());
    }

    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/precompelete/{matchId}")
    @ApiOperation(value = "得到所有比赛简易信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView precompelete(@PathVariable Long matchId) {
        return new ModelAndView("match/preCompelete","match", matchService.findOne(matchId));
    }

    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/docompelete")
    @ApiOperation(value = "得到所有比赛简易信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ResponseEntity<DwzMessage> doCompelete(HistoryMatch historyMatch) {

    	//更新赛事实体
        MatchDetail matchDetail = new MatchDetail();
        matchDetail.setId(historyMatch.getId());
        matchDetail.setCompelete(true);

        
        //更新队伍信息
        Team team1 = teamService.findOne(historyMatch.getTeam1Id());
        Team team2 = teamService.findOne(historyMatch.getTeam2Id());
        
        if(historyMatch.getTeam1score()>historyMatch.getTeam2score()){
        	historyMatch.setWinteamId(historyMatch.getTeam1Id());
        	
        	team1.setWinRate((team1.getWinRate()*team1.getMatchTime()+1)/(team1.getMatchTime()+1));
        	team2.setWinRate((team2.getWinRate()*team2.getMatchTime())/(team2.getMatchTime()+1));
        }else{
        	historyMatch.setWinteamId(historyMatch.getTeam2Id());
        	
        	team2.setWinRate((team2.getWinRate()*team2.getMatchTime()+1)/(team2.getMatchTime()+1));
        	team1.setWinRate((team1.getWinRate()*team1.getMatchTime())/(team1.getMatchTime()+1));
        }
        
        team1.setMatchTime(team1.getMatchTime()+1);
        team2.setMatchTime(team2.getMatchTime()+1);
        
        matchService.updateByPrimaryKeySelective(matchDetail);
        historyMatchService.save(historyMatch);
        teamService.updateByPrimaryKeySelective(team1);
        teamService.updateByPrimaryKeySelective(team2);       
        
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setMessage("操作成功");
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/match/uncompelete");
        dwzMessage.setStatusCode(200);
        
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }

    /**
     * 服务端预处理添加事件
     * @param
     * @return
     */
    @RequestMapping(value="/preAdd")
    @ApiOperation(value = "添加赛事", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public String preAdd(){
        return "match/addMatch";
    }  
    
    /**
     * 服务端添加赛事
     * @param matchDetail
     * @return
     */
    @RequestMapping(value="/add")
    @ApiOperation(value = "添加赛事", notes = "add new match", httpMethod = "POST", response = DwzMessage.class)
    public ResponseEntity<DwzMessage> applyForMatch(MatchDetail matchDetail){
    	matchDetail.setCompelete(false);
    	matchService.save(matchDetail);
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setStatusCode(200);
        dwzMessage.setMessage("添加成功");
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/match/uncompelete");
        dwzMessage.setNavTabId("赛事详情");
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }



    /**
     * 用户完善赛事信息
     * @return
     */
    @RequestMapping(value="/preUpdate/{id}")
    @ApiOperation(value = "预处理更新赛事信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView preUpdate(@PathVariable  Long id){
        matchService.findOne(id);
        return new ModelAndView("match/preUpdate","match", matchService.findOne(id));
    }

    /**
     * 服务端或者用户端更新赛事
     * @return
     */
    @RequestMapping(value="/update")
    @ApiOperation(value = "更新赛事", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ResponseEntity<DwzMessage> update(MatchDetail matchDetail){
    	matchService.updateByPrimaryKeySelective(matchDetail);
    	 DwzMessage dwzMessage = new DwzMessage();
    	 dwzMessage.setMessage("添加成功");
         dwzMessage.setCallbackType("forward");
         dwzMessage.setForwardUrl("/match/uncompelete");
         dwzMessage.setNavTabId("赛事详情");
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }

    /**
     * 服务端删除赛事
     * @return     
     */
    @RequestMapping(value="/delete/{id}")
    @ApiOperation(value = "删除赛事", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ResponseEntity<DwzMessage> allMatchApply(@PathVariable Long id){
    	matchService.delete(id);
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/match/all");
        dwzMessage.setStatusCode(200);
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }

    /**
     * 服务端删除赛事
     * @return
     */
    @RequestMapping(value="/abort/{id}")
    @ApiOperation(value = "终止赛事", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ResponseEntity<DwzMessage> abortMatch(@PathVariable Long id){
        matchService.delete(id);
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/match/all");
        dwzMessage.setStatusCode(200);
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }



}
